package com.hgsoft.pubsv.component.base.token.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hgsoft.pubsv.component.base.Constants;
import com.hgsoft.pubsv.component.base.RedisKeyConstants;
import com.hgsoft.pubsv.component.base.context.ApiReqContextHolder;
import com.hgsoft.pubsv.component.base.dto.BaseApiReqDto;
import com.hgsoft.pubsv.component.base.dto.OAuthToken;
import com.hgsoft.pubsv.component.base.execption.ApiCodeException;
import com.hgsoft.pubsv.component.base.redis.RedisStringUtil;
import com.hgsoft.pubsv.component.base.token.IOAuthCacheTokenService;

import cn.hutool.json.JSONUtil;

@Service
public class IOAuthCacheTokenServiceImpl implements IOAuthCacheTokenService {

	@Autowired
	private RedisStringUtil redisStringUtil;

	@Override
	public void delAppOauthToken(String clientKey, String accessNo) {
		redisStringUtil.del(getAppClientKey(clientKey, accessNo));
	}

	@Override
	public void addAppOauthToken(OAuthToken authToken, long time) {
		// 添加缓存
		redisStringUtil.set(getAppClientKey(authToken.getClientKey(), authToken.getAccessNo()),
				JSONUtil.toJsonStr(authToken), time);
	}

	/**
	 * APP缓存key
	 *
	 * @param clientKey
	 * @return
	 */
	private String getAppClientKey(String clientKey, String accessNo) {
		return RedisKeyConstants.APP_OAUTH_TOKEN_PRE + accessNo + RedisKeyConstants.SPLIT + clientKey;
	}

	@Override
	public String getAppUserTokenId(String clientKey, String accessNo) {
		String tokenStr = redisStringUtil.get(getAppClientKey(clientKey, accessNo));
		if (StringUtils.isBlank(tokenStr)) {
			throw new ApiCodeException(Constants.ERR_TOKEN_INVALID, Constants.ERR_TOKEN_INVALID_NAME);
		}
		OAuthToken authToken = JSONUtil.toBean(tokenStr, OAuthToken.class);
		return authToken.getToken();
	}

	@Override
	public String getAppUserId(String clientKey, String accessNo) {
		String tokenStr = redisStringUtil.get(getAppClientKey(clientKey, accessNo));
		if (StringUtils.isBlank(tokenStr)) {
			throw new ApiCodeException(Constants.ERR_TOKEN_INVALID, Constants.ERR_TOKEN_INVALID_NAME);
		}
		OAuthToken authToken = JSONUtil.toBean(tokenStr, OAuthToken.class);
		return authToken.getUserId();
	}

	@Override
	public OAuthToken getAppUserToken(String clientKey, String accessNo) {
		String tokenStr = redisStringUtil.get(getAppClientKey(clientKey, accessNo));
		if (StringUtils.isBlank(tokenStr)) {
			throw new ApiCodeException(Constants.ERR_TOKEN_INVALID, Constants.ERR_TOKEN_INVALID_NAME);
		}
		OAuthToken authToken = JSONUtil.toBean(tokenStr, OAuthToken.class);
		return authToken;
	}

	@Override
	public String getAppUserTokenId() {
		BaseApiReqDto apiReqDto = ApiReqContextHolder.getBaseApiReqDto();
		if (apiReqDto != null && StringUtils.isNotBlank(apiReqDto.getAccessNo())
				&& StringUtils.isNotBlank(apiReqDto.getClientKey())) {
			return getAppUserTokenId(apiReqDto.getClientKey(), apiReqDto.getAccessNo());
		}
		return null;
	}

	@Override
	public String getAppUserId() {
		BaseApiReqDto apiReqDto = ApiReqContextHolder.getBaseApiReqDto();
		if (apiReqDto != null && StringUtils.isNotBlank(apiReqDto.getAccessNo())
				&& StringUtils.isNotBlank(apiReqDto.getClientKey())) {
			return getAppUserId(apiReqDto.getClientKey(), apiReqDto.getAccessNo());
		}
		return null;
	}

	@Override
	public OAuthToken getAppUserToken() {
		BaseApiReqDto apiReqDto = ApiReqContextHolder.getBaseApiReqDto();
		if (apiReqDto != null && StringUtils.isNotBlank(apiReqDto.getAccessNo())
				&& StringUtils.isNotBlank(apiReqDto.getClientKey())) {
			return getAppUserToken(apiReqDto.getClientKey(), apiReqDto.getAccessNo());
		}
		return null;
	}

}
